package com.ruoyi.zsyh.controller.applet.user;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.ImageUtils;
import com.ruoyi.common.utils.UrlUtils;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 首页banner管理Controller
 * 
 * @author zhangkm
 * @date 2022-2-21
 */
@Controller
@RequestMapping("/applet/zsyh/userInfo")
public class ZsyhUserInfoAppletController extends BaseController
{

    @Autowired
    private IZsyhUserMessageService zsyhUserMessageService;

    @Autowired
    private IZsyhUserHelpService zsyhUserHelpService;

    @Autowired
    private IZsyhUserCooperationService zsyhUserCooperationService;

    @Autowired
    private IZsyhPlatformUserService zsyhPlatformUserService;

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhWebArticleService zsyhWebArticleService;

    /**
     * 根据用户id获取用户信息
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getUserById")
    @ResponseBody
    public AjaxResult appletsLogin(String userId)throws Exception {

        ZsyhPlatformUser user = zsyhPlatformUserService.selectZsyhPlatformUserById(userId);
        if(user != null){
            return AjaxResult.success("操作成功", user);
        }
        return AjaxResult.error("userId不存在");
    }

    /**
     * 首页消息
     * @return
     * @throws Exception
     */
    @RequestMapping("/getHomeMessage")
    @ResponseBody
    public AjaxResult getHomeMessage(String userId)throws Exception
    {
        ZsyhUserMessage message = new ZsyhUserMessage();
        Map map = new HashMap();
        map.put("type", "首页消息");
        map.put("userId", userId);
//        map.put("beginPos", 0);
//        map.put("pageSize", 20);
        message.setParams(map);
        List<ZsyhUserMessage> list = zsyhUserMessageService.selectZsyhUserMessageList(message);

        List<ZsyhUserMessage> newList = new ArrayList<>();
        Random random = new Random();
        for(int i=0; i<=20; i++){
            int index = random.nextInt(list.size());
            newList.add(list.get(index));
        }

        return AjaxResult.success("操作成功", newList);
    }

    /**
     * 个人中心消息
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUserMessage")
    @ResponseBody
    public AjaxResult getUserMessage(String userId)throws Exception
    {
        ZsyhUserMessage message = new ZsyhUserMessage();
        message.setUserId(userId);
        message.setMessageType("2");
        List<ZsyhUserMessage> list = zsyhUserMessageService.selectZsyhUserMessageList(message);
        return AjaxResult.success("操作成功", list);
    }

    /**
     * 店铺消息列表
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUserStoreMessage")
    @ResponseBody
    public AjaxResult getUserStoreMessage(String userId)throws Exception
    {
        ZsyhUserMessage message = new ZsyhUserMessage();
        message.setUserId(userId);
        message.setMessageType("3");
        List<ZsyhUserMessage> list = zsyhUserMessageService.selectZsyhUserMessageList(message);
        return AjaxResult.success("操作成功", list);
    }

    /**
     * 未读的消息通知的条数
     * @return
     */
    @RequestMapping("/getUserMessageNum")
    @ResponseBody
    public AjaxResult getUserMessageNum(String userId)throws Exception
    {
        if(EmptyUtils.isNotEmpty(userId)){
            ZsyhUserMessage message = new ZsyhUserMessage();
            message.setUserId(userId);
            message.setMessageStatus("0");
            List<ZsyhUserMessage> list = zsyhUserMessageService.selectZsyhUserMessageList(message);
            return success("成功", list.size());
        }else{
            return success("成功", "0");
        }

    }

    /**
     * 阅读消息
     * @return
     */
    @RequestMapping("/updateUserMessageById")
    @ResponseBody
    public AjaxResult updateUserMessageById(String id)throws Exception
    {
        ZsyhUserMessage message = new ZsyhUserMessage();
        message.setId(id);
        message.setMessageStatus("1");
        zsyhUserMessageService.updateZsyhUserMessage(message);
        return success("成功");
    }


    /**
     * 帮助中心列表
     * @param helpType  分类 1普通客户2配件店3维修师傅 4电器店 5市县管理商 6大区会长 7厂家
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUserHelpList")
    @ResponseBody
    public AjaxResult getUserHelpList(String helpType)throws Exception
    {
        ZsyhUserHelp help = new ZsyhUserHelp();
        help.setHelpType(helpType);
        return success("成功", zsyhUserHelpService.selectZsyhUserHelpList(help));
    }

    /**
     * 获取帮助中心的帮助详情
     * @param helpId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUserHelpDetails")
    @ResponseBody
    public AjaxResult getUserHelpDetails(String helpId)throws Exception
    {
        return success("成功", zsyhUserHelpService.selectZsyhUserHelpById(helpId));
    }

    /**
     * 提交合作申请
     * @param userId
     * @param cooperaPerson
     * @param cooperaPhone
     * @param coopearAddress
     * @param cooperaContent
     * @param cooperaIndustry
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @RequestMapping("/saveCooperation")
    @ResponseBody
    public AjaxResult saveCooperation(String userId, String cooperaPerson, String cooperaPhone, String coopearAddress, String cooperaContent, String cooperaIndustry)throws Exception
    {
        ZsyhUserCooperation cooperation = new ZsyhUserCooperation();
        cooperation.setId(IdWorker.getId());
        cooperation.setUserId(userId);
        cooperation.setCooperaPerson(cooperaPerson);
        cooperation.setCooperaPhone(cooperaPhone);
        cooperation.setCoopearAddress(coopearAddress);
        cooperation.setCooperaContent(cooperaContent);
        cooperation.setCooperaIndustry(cooperaIndustry);
        zsyhUserCooperationService.insertZsyhUserCooperation(cooperation);
        return success("成功");
    }

    /**
     * 获取官网知识列表
     * @param isTop
     * @param type
     * @return
     * @throws Exception
     */
    @RequestMapping("/getWebArticleList")
    @ResponseBody
    public AjaxResult getWebArticleList(String isTop, String isHot, String type, String pageNum, String pageSize, String key)throws Exception
    {
        ZsyhWebArticle article = new ZsyhWebArticle();
        article.setIsTop(isTop);
        article.setIsHot(isHot);
        article.setArtileType(type);
        Map map = new HashMap();
        int num = 0;
        int size = 10;
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            num = Integer.valueOf(pageNum);
            size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
        }
        map.put("beginPos", num);
        map.put("pageSize", size);
        article.setArticleTitle(key);
        article.setParams(map);
        List<ZsyhWebArticle> list = zsyhWebArticleService.selectZsyhWebArticleList(article);

        article.setParams(null);
        List<ZsyhWebArticle> listAll = zsyhWebArticleService.selectZsyhWebArticleList(article);
        int listNum = listAll.size();
        map = new HashMap();
        map.put("list", list);
        map.put("listNum", listNum);

        return success("成功", map);
    }

    /**
     * 获取官网知识详情
     * @return
     * @throws Exception
     */
    @RequestMapping("/getWebArticleById")
    @ResponseBody
    public AjaxResult getWebArticleById(String id)throws Exception
    {

        ZsyhWebArticle article = zsyhWebArticleService.selectZsyhWebArticleById(id);
        article.setArticleViews(article.getArticleViews() + 1);

        //获取当前文章的上一个
        ZsyhWebArticle article1 = new ZsyhWebArticle();
        Map map1 = new HashMap();
        map1.put("createTime1", article.getCreateTime());
        article1.setParams(map1);
        article1.setArtileType(article.getArtileType());
        List<ZsyhWebArticle> articlelist1 = zsyhWebArticleService.selectZsyhWebArticleList(article1);

        //获取当前文章的下一个
        ZsyhWebArticle article2 = new ZsyhWebArticle();
        Map map2 = new HashMap();
        map2.put("createTime2", article.getCreateTime());
        article2.setParams(map2);
        article2.setArtileType(article.getArtileType());
        List<ZsyhWebArticle> articlelist2 = zsyhWebArticleService.selectZsyhWebArticleList(article2);

        Map map = new HashMap();
        map.put("article", article);
        map.put("articleUp", new HashMap());
        map.put("articleDown", new HashMap());
        if(articlelist1.size() > 0){
            map1 = new HashMap();
            map1.put("id", articlelist1.get(0).getId());
            map1.put("articleTitle", articlelist1.get(0).getArticleTitle());
            map.put("articleUp", map1);
        }
        if(articlelist2.size() > 0){
            map2 = new HashMap();
            map2.put("id", articlelist2.get(0).getId());
            map2.put("articleTitle", articlelist2.get(0).getArticleTitle());
            map.put("articleDown", map2);
        }
        ZsyhWebArticle _article = new ZsyhWebArticle();
        _article.setId(article.getId());
        _article.setArticleViews(article.getArticleViews());
        zsyhWebArticleService.updateZsyhWebArticle(_article);

        return success("成功", map);
    }


    /**
     * 压缩图片 -- 根据文件夹压缩里边的图片
     * @param fileUrl
     * @return
     * @throws Exception
     */
    @RequestMapping("/compressPictures")
    @ResponseBody
    public AjaxResult compressPictures(String fileUrl)throws Exception
    {
        ImageUtils imageUtils = new ImageUtils();
        imageUtils.covert(fileUrl);
        return success();
    }
}

